<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" type="text/css" href="css/style1.css">
    <link rel="stylesheet" type="text/css" href="css/toastr.min.css">
</head>
<body>
    <div class="header">
        <button class="btn0">一键开馆</button>
        <button class="btn1">一键闭馆</button>
    </div>
    <div class="tab-wrapper">
        <ul class="tab">
            <li class="tab-item active">PC</li>
            <li class="tab-item">灯</li>
            <li class="tab-item">主机</li>
            <li class="tab-item">投影仪</li>
            <li class="tab-item">灯箱</li>
            <li class="tab-item">备用</li>
        </ul>
        <div class="tab-content">
            <div class="content show">
                <div id="tab" class="content-wrapper">
                    
                </div>
            </div>
            <div class="content">
                 <div id="tab1" class="content-wrapper">
                    
                </div>
            </div>
            <div class="content">
                 <div id="tab2" class="content-wrapper">
                    
                </div>
            </div>
            <div class="content">
                 <div id="tab4" class="content-wrapper">
                    
                </div>
            </div>
            <div class="content">
                 <div id="tab5" class="content-wrapper">
                    
                </div>
            </div>
            <div class="content">
                 <div id="tab6" class="content-wrapper">
                    
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script src="../js/jquery-3.4.1.min.js"></script>
<script src="../js/toastr.min.js"></script>
<script src="../js/sweetalert.min.js"></script>

<script type="text/javascript">
    $(function (){
        $(".tab li").click(function (){
            $(this).addClass("active").siblings().removeClass("active");
            var index = $(this).index();
            $('.tab-content').find('.content').eq(index).addClass("show").siblings().removeClass("show");
        });
        var divHeight = document.documentElement.clientHeight - 146
        $(".content-wrapper").height(divHeight)
        // alert(divHeight)
    });
</script>

<script type="text/javascript">
    $(document).ready(function() {
        $.get("/api/index_m",
        function(data,status) {
            // console.log(data);
            var o = eval("(" + data + ")");  // 转换为json对象
            if(o['code'] == '10001') {
                console.log('未登录状态');
                window.location.href='login.html';
                return;
            }
        });

        // 获取 主机列表
        $.get("/api/getHosts",
        function(data,status) {
            // console.log(data);
            var o = eval("(" + data + ")");  // 转换为json对象
            o.data.forEach(function(item, index){
                // console.log("index[" + index + "]: " + item.name);
                if(item.status == '1') {
                    $('#tab').append('<div class="content-item2"><div class="circle host" data-id=\'' +JSON.stringify(item)+ '\'><img class="play" src="img/stop.png"><div class="img-wrapper"><img class="yes" src="img/check_yes.png"></div></div><div style="height:60px"><p class="text1">'+item.name+'</p><p class="text2">'+item.ip+'</p></div></div>');
                } else if (item.status == '0') {
                    $('#tab').append('<div class="content-item2"><div class="circle status2 host" data-id=\'' +JSON.stringify(item)+ '\'><img class="play" src="img/play.png"><div class="img-wrapper"><img class="yes" src="img/check_false.png"></div></div><div style="height:60px"><p class="text1">'+item.name+'</p><p class="text2">'+item.ip+'</p></div></div>');
                } else {
                    $('#tab').append('<div class="content-item2"><div class="circle status3 host" data-id=\'' +JSON.stringify(item)+ '\'><img class="play" src="img/unknow1.png"><div class="img-wrapper"><img class="yes" src="img/check_unkonw.png"></div></div><div style="height:60px"><p class="text1">'+item.name+'</p><p class="text2">'+item.ip+'</p></div></div>');
                }

            });

            // PC 点击
            $('[class="circle host"]').unbind('click').click(function() {
                // console.log($(this).data('id'));
                // var _o = JSON.parse($(this).attr('data-id'));
                // console.log(_o);

                var _id = $(this).data('id').id;
                var _sys = $(this).data('id').sys;
                var _name = $(this).data('id').name;
                var _ip = $(this).data('id').ip;
                var _cmd = $(this).data('id').cmd;

                swal({
                  text: "确认关机" + _name + "吗？",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                    if(_sys == 'win') {
                        $.ajax({
                            url: 'http://' + _ip + ':8001',
                            type: 'POST',
                            dataType: 'jsonp',  //数据格式为jsonp
                            jsonp: 'callback',  //函数变量名
                            jsonpCallback: 'list',
                            success:function(data){  //成功的回调函数
                                console.log(data);
                                var _swal = swal({
                                  text: "正在关机，请不要关闭浏览器!",
                                  type: "warning",
                                });
                                setTimeout(function() {
                                    $.ajax({
                                        method: 'get',
                                        url: '/api/checkHost?ip=' + _ip + '&id=' + _id,
                                        success: function (data) {
                                            console.log(data);
                                            if(data == 0 ) {
                                                swal({
                                                  text: _name + "电脑关机成功!",
                                                  type: "success",
                                                });
                                            } else {
                                                swal({
                                                  text: _name + "电脑关机失败!",
                                                  type: "error",
                                                });
                                            }
                                        }
                                    });
                                    location.reload();
                                }, 8000);
                            },
                            error: function (e) {
                                console.log(e)
                            }
                        });
                    } else {
                       $.ajax({
                            method: 'get',
                            url: '/api/hostShutdown?id=' + _id + '&ip=' + _ip + '&cmd=' + _cmd,
                            success: function (data) {
                                console.log(data);
                                var _swal = swal({
                                  text: "正在关机，请不要关闭浏览器!",
                                  type: "warning",
                                });
                                setTimeout(function() {
                                    $.ajax({
                                        method: 'get',
                                        url: '/api/checkHost?ip=' + _ip + '&id=' + _id,
                                        success: function (data) {
                                            console.log(data);
                                            if(data == 0 ) {
                                                swal({
                                                  text: _name + "电脑关机成功!",
                                                  type: "success",
                                                });
                                            } else {
                                                swal({
                                                  text: _name + "电脑关机失败!",
                                                  type: "error",
                                                });
                                            }
                                        }
                                    });
                                    location.reload();
                                }, 8000);
                            }
                        });
                    }
                  }
                });
            });
        });

        // 获取 主机列表
        $.get("/api/getDevices",
        function(data,status) {
            // console.log(data);
            var o = eval("(" + data + ")");  // 转换为json对象
            o.data.forEach(function(item, index){
                // console.log("index[" + index + "]: " + item.name);
                if(item.status == 12) {
                    $('#tab' + item.type).append('<div class="content-item2"><div class="circle power " data-id=\'' +JSON.stringify(item)+ '\'><span class="span">开</span><div class="img-wrapper"><img class="yes" src="img/check_yes.png"></div></div><div style="height:60px"><p class="text1">'+item.name+'</p><p class="text2">'+item.code+'</p></div></div>');
                } else if (item.status == 11) {
                    $('#tab'+ item.type).append('<div class="content-item2"><div class="circle status2  power" data-id=\'' +JSON.stringify(item)+ '\'><span class="span">关</span><div class="img-wrapper"><img class="yes" src="img/check_false.png"></div></div><div style="height:60px"><p class="text1">'+item.name+'</p><p class="text2">'+item.code+'</p></div></div>');
                } else {
                    $('#tab'+ item.type).append('<div class="content-item2"><div class="circle status3  power" data-id=\'' +JSON.stringify(item)+ '\'><span class="span">?</span><div class="img-wrapper"><img class="yes" src="img/check_unkonw.png"></div></div><div style="height:60px"><p class="text1">'+item.name+'</p><p class="text2">'+item.code+'</p></div></div>');
                }
            });

            // 其他 点击
            $('.power').unbind('click').click(function() {
                console.log($(this).data('id'));
                var _id = $(this).data('id').id;
                var _name = $(this).data('id').name;
                var _cmd = $(this).data('id').cmd;
                var _status = $(this).data('id').status;
                if (_status == 12) {
                    $.ajax({
                        method: 'get',
                        url: '/api/sentCmd?id=' + _id + '&flag=11&cmd=' + _cmd,
                        success: function (data) {
                            // console.log(data);
                            var o = eval("(" + data + ")");  // 转换为json对象
                            // console.log(o.code);
                            
                            if(o.code == '200') {
                                swal('关闭' + _name + '的电源成功！')
                                .then((value) => {
                                  location.reload();
                                });
                            }else{
                                swal('关闭' + _name + '的电源失败！')
                                .then((value) => {
                                  location.reload();
                                });
                            }
                            
                        }
                    });
                } else {
                    $.ajax({
                    method: 'get',
                    url: '/api/sentCmd?id=' + _id + '&flag=12&cmd=' + _cmd,
                    success: function (data) {
                        // console.log(data);
                        var o = eval("(" + data + ")");  // 转换为json对象
                        // console.log(o.code);
                        
                        if(o.code == '200') {
                            swal('打开' + _name + '的电源成功！')
                            .then((value) => {
                              location.reload();
                            });
                        }else{
                            swal('打开' + _name + '的电源失败！')
                            .then((value) => {
                              location.reload();
                            });
                        }
                    }
                });
                }
                
            });
            
        });

        //一键开馆
        $('.btn0').unbind('click').click(function() {
            swal({
              text: "确认打开全部的电源吗？",
              buttons: true,
              dangerMode: true,
            })
            .then((willDelete) => {
              if (willDelete) {
                $.ajax({
                    method: 'get',
                    url: '/api/getDevices',
                    success: function (data) {
                        swal('正在发送命令，请保持手机解锁并不要关闭浏览器！', '', 'warning');
                        // console.log(data);
                        var o = eval("(" + data + ")");  // 转换为json对象
                        // console.log(o.data);
                        var i = 0;
                        var t = setInterval(function() {
                            var _id = o.data[i].id;
                            var _name = o.data[i].name;
                            var _cmd = o.data[i].cmd;
                            $.ajax({
                                method: 'get',
                                url: '/api/sentCmd?id=' + _id + '&flag=12&cmd=' + _cmd,
                                success: function (data) {
                                    // console.log(data);
                                    var o = eval("(" + data + ")");  // 转换为json对象
                                    // console.log(o.code);
                                    
                                    if(o.code == '200') {
                                        toastr.success('打开' + _name + '打开' + _name + '的电源成功！<br/>请勿关闭浏览器，正在打开下一个', '', {positionClass:"toast-top-center"});
                                    } else{
                                        toastr.error('打开' + _name + '的电源失败！<br/>请勿关闭浏览器，正在打开下一个', '', {positionClass:"toast-top-center"});
                                    }
                                    

                                }
                            }); 
                            i += 1;
                            if(i > o.data.length - 1) {
                                clearInterval(t);
                                $.ajax({
                                    method: 'get',
                                    url: '/api/updateHost',
                                    success: function (data) {
                                        console.log(data);
                                    }
                                });
                                location.reload();
                            }
                        }, 3000);
                    }
                });
              }
            });
        });

        // 一键闭馆
        $('.btn1').unbind('click').click(function() {
            swal({
              text: "确认关闭全部的电源吗？",
              buttons: true,
              dangerMode: true,
            })
            .then((willDelete) => {
              if (willDelete) {
                $.ajax({
                    method: 'get',
                    url: '/api/getHosts',
                    success: function (data) {
                        // console.log(data);
                        var o = eval("(" + data + ")");  // 转换为json对象
                        console.log(o.data);
                        swal('正在发送命令，请保持手机解锁并不要关闭浏览器！', '', 'warning');
                        o.data.forEach(function (item, index) {
                            // console.log(item);
                            if(item.sys == 'win') {
                                $.ajax({
                                    url: 'http://' + item.ip + ':8001',
                                    type: 'POST',
                                    dataType: 'jsonp',  //数据格式为jsonp
                                    jsonp: 'callback',  //函数变量名
                                    jsonpCallback: 'list',
                                    success:function(data){  //成功的回调函数
                                        console.log(data);
                                    },
                                    error: function (e) {
                                        console.log(e)
                                    }
                                });
                            } else {
                                $.ajax({
                                    method: 'get',
                                    url: '/api/hostShutdown?id=' + item.id + '&ip=' + item.ip + '&cmd=' + item.cmd,
                                    success: function (data) {
                                        console.log(data);
                                    }
                                });
                            }   
                        });

                        var i = 0;
                        var t = setInterval(function() {
                            var _id = o.data[i].id;
                            var _ip = o.data[i].ip;
                            var _name = o.data[i].name;
                            $.ajax({
                                method: 'get',
                                url: '/api/checkHost?ip=' + _ip + '&id=' + _id,
                                success: function (data) {
                                    console.log(data);
                                    if(data == 0 ) {
                                        toastr.success( _name + "电脑关机成功!", '', {positionClass:"toast-top-center"});
                                    } else {
                                        toastr.error(_name + "电脑关机失败!", '', {positionClass:"toast-top-center"});
                                    }
                                }
                            });

                            i += 1;
                            if(i > o.data.length - 1) {
                                clearInterval(t);
                                var _swal1 = swal({
                                      text: '全部主机关闭，正在发送关闭电源命令！',
                                      type: "success",
                                });
                                swal('全部主机关闭，正在发送关闭电源命令！请保持手机解锁并不要关闭浏览器！', '', 'warning');

                                $.ajax({
                                    method: 'get',
                                    url: '/api/getDevices',
                                    success: function (data) {
                                        // console.log(data);
                                        var o = eval("(" + data + ")");  // 转换为json对象
                                        console.log(o.data);

                                        var ii = 0;
                                        var tt = setInterval(function() {
                                            var _id = o.data[ii].id;
                                            var _name = o.data[ii].name;
                                            var _cmd = o.data[ii].cmd;
                                            $.ajax({
                                                method: 'get',
                                                url: '/api/sentCmd?id=' + _id + '&flag=11&cmd=' + _cmd,
                                                success: function (data) {
                                                    // console.log(data);
                                                    var o = eval("(" + data + ")");  // 转换为json对象
                                                    // console.log(o.code);
                                                    
                                                    if(o.code == '200') {
                                                        toastr.success('关闭' + _name + '的电源成功！', '', {positionClass:"toast-top-center"});
                                                    }else{
                                                        toastr.error('关闭' + _name + '的电源失败！', '', {positionClass:"toast-top-center"});
                                                    }
                                                }
                                            }); 
                                            ii += 1;
                                            if(ii > o.data.length - 1){
                                                clearInterval(tt);
                                                location.reload();
                                            }
                                        },3000);
                                    }
                                });
                            }
                        }, 8000);
                    }
                });
            }
        });
    });

    });
</script>
